"""
    装饰器：登录状态校验
"""
import json

import jwt
from django.http import JsonResponse

from midproject import settings
from course.models import Student


def logging_check(func):
    def wrapper(self, request, *args, **kwargs):
        data = json.loads(request.body)
        request.mydata = data
        token = data.get("token")
        if token != "token":
            return JsonResponse(
                {
                    "code": 403,
                    "error": "错误请求"
                }
            )
        username = data.get("username")
        request.myuser = Student.objects.get(username=username)
        return func(self, request, *args, **kwargs)

    return wrapper
